import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'// Progress 进度条样式
import { GETUSERINFO } from '@/store/mutation-types'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login')
  },
  // {
  //   path: '/homepage',
  //   name: 'Homepage',
  //   component: () => import(/* webpackChunkName: "homepage" */ '../views/homepage/Homepage')
  // },
  // {
  //   path: '/pms/product',
  //   name: 'Homepage',
  //   component: () => import(/* webpackChunkName: "homepage" */ '@/views/pms/product')
  // }
]

const createRouter = () => new VueRouter({
  routes
})

const router = createRouter();

// 重置路由
export const resetRouter = () => {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher;
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const addDynamicRoute = () => {
  let menus = store.getters.menus;
  // 生成菜单
  let i = 0;
  let asnycRouter = [];
  for (let key in menus) {
    asnycRouter.push({
      path: '/' + menus[key].name,
      name: menus[key].name,
      meta: { title: menus[key].title },
      component: () => import(/* webpackChunkName: "homepage" */ '../views/homepage/Homepage'),
      redirect: '/' + menus[key].name + '/' + menus[key].children[0].name,
      children: []
    });
    for (let j = 0; j < menus[key].children.length; j++) {
      asnycRouter[i].children.push({
        path: '/' + menus[key].name + '/' + menus[key].children[j].name,
        name: menus[key].children[j].name,
        meta: { title: menus[key].children[j].title },
        component: () => import('@/views/' + menus[key].name + '/' + menus[key].children[j].name)
      })
    }
    i++;
  }
  
  // console.log(asnycRouter);
  router.addRoutes([
    {
      path: '/home',
      name: 'home',
      component: () => import(/* webpackChunkName: "homepage" */ '../views/homepage/Homepage'),
      children: [{
        path: '/index',
        name: 'index',
        meta: {
          title: '首页'
        },
        component: () => import(/* webpackChunkName: "index" */ '../views/index/index')
      }]
    }, ...asnycRouter
  ])
}

// 动态路由
router.beforeEach((to, from, next) => {
  NProgress.start();
  let token = localStorage.getItem("token")
  let userinfo = store.state.user.userinfo;
  if (to.path === '/homepage' && from.path === '/login' && token) {
    addDynamicRoute();
    next('/index');
  } else if (token && !userinfo.menus) {
    store.dispatch(GETUSERINFO).then(res => {
      addDynamicRoute();
      next(to);
    })
  } else if (token && to.path === '/login') {
    next(from)
  } else if (!token && to.path !== '/login') {
    next('/login')
  } else {
    next();
  }
})

router.afterEach((to, from) => {
  NProgress.done();
})

export default router
